<?php

namespace operation;
use think\Request;
use app\sports_admin\model\OperationLog as OperationLogModel;
use think\Session;
use Zhuzhichao\IpLocationZh\Ip;
/**
 * 用于记录用户操作
 */
class Operation
{
    //请求ip
    private $ip;
    //请求url
    private $url;
    //请求的控制器名称
    private $controller;
    //请求的方法名称
    private $action;
    //请求参数
    private $params;
    //浏览器类型
    private $browser;
    //操作人
    private $operation_username;
    //ip地址所在地
    private $request_ip_address;
    public function __construct()
    {
        //请求信息
        $info = Request::instance();
        $this->ip = sprintf('%u',ip2long($info->ip()));
        $this->url = $info->url();
        $this->controller = $info->controller();
        $this->action = $info->action();
        $this->params = $info->param();
        $this->browser = $this->getBrowser();
        $this->operation_username = Session::get('sport_admin.name');
        $this->request_ip_address = $this->getIpCity($info->ip());
    }
    /*
     *获取ip地址所在地
     *@param $ip ip地址
     */
    public function getIpCity($ip)
    {
        $ipAddress = Ip::find($ip);
        return $ipAddress[1].$ipAddress[2];
    }
    /**
     * 拼装操作信息
     * @param 操作名称
    */
    public function operationInfo($operationName)
    {
        $info = [
            'username' => $this->operation_username,'request_url'=>$this->url,
            'request_controller'=>$this->controller,'request_action'=>$this->action,
            'request_params'=>json_encode($this->params),'request_ip'=>$this->ip,
            'browser'=>$this->browser,'operation'=>$operationName,
            'request_ip_address'=>$this->request_ip_address
        ];
        return $info;
    }
    /**
     * 查询操作记录
    */
    public function select()
    {
        OperationLogModel::create($this->operationInfo('查询'));
    }
    /**
     * 增加操作记录
    */
    public function insert()
    {
        OperationLogModel::create($this->operationInfo('增加'));
    }
    /**
     * 修改操作记录
    */
    public function update()
    {
        OperationLogModel::create($this->operationInfo('修改'));
    }
    /**
     * 删除操作记录
    */
    public function delete()
    {
        OperationLogModel::create($this->operationInfo('删除'));
    }
    //获取浏览器类型
    public function getBrowser()
    {
        $user_OSagent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "Maxthon(Microsoft IE)";
        } elseif (strpos($user_OSagent, "Maxthon 2.0")) {
            $visitor_browser = "Maxthon 2.0";
        } elseif (strpos($user_OSagent, "Maxthon")) {
            $visitor_browser = "Maxthon";
        } elseif (strpos($user_OSagent, "Edge")) {
            $visitor_browser = "Edge";
        } elseif (strpos($user_OSagent, "Trident")) {
            $visitor_browser = "IE";
        } elseif (strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "IE";
        } elseif (strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "MSIE 较高版本";
        } elseif (strpos($user_OSagent, "NetCaptor")) {
            $visitor_browser = "NetCaptor";
        } elseif (strpos($user_OSagent, "Netscape")) {
            $visitor_browser = "Netscape";
        } elseif (strpos($user_OSagent, "Chrome")) {
            $visitor_browser = "Chrome";
        } elseif (strpos($user_OSagent, "Lynx")) {
            $visitor_browser = "Lynx";
        } elseif (strpos($user_OSagent, "Opera")) {
            $visitor_browser = "Opera";
        } elseif (strpos($user_OSagent, "MicroMessenger")) {
            $visitor_browser = "微信浏览器";
        } elseif (strpos($user_OSagent, "Konqueror")) {
            $visitor_browser = "Konqueror";
        } elseif (strpos($user_OSagent, "Mozilla/5.0")) {
            $visitor_browser = "Mozilla";
        } elseif (strpos($user_OSagent, "Firefox")) {
            $visitor_browser = "Firefox";
        } elseif (strpos($user_OSagent, "U")) {
            $visitor_browser = "Firefox";
        } else {
            $visitor_browser = "其它";
        }
        return $visitor_browser;
    }
}